package com.rigsofrods.truckeditor.geometry;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.fixedfunc.GLPointerFunc;

public class Mesh{
	private BoundingBox3D aabb = null;
	private FloatBuffer vertices;
	private IntBuffer indices;
	private int numElements = 0;
	private int met;
	
	public enum MeshElementType{
		ME_LINE, ME_TRIANGLE;
	}
	
	public Mesh(FloatBuffer vertices, IntBuffer indices, MeshElementType met) {
		super();
		this.vertices = vertices;
		this.indices = indices;
		aabb = new BoundingBox3D();
		vertices.rewind();
		int l = vertices.remaining();
		numElements = l / 3;//(2 * 3);
		if (met == MeshElementType.ME_LINE) {
			this.met = GL.GL_LINES;
		} else {
			//numElements = l / (3 * 3);
			this.met = GL.GL_TRIANGLES;
		}
		for (int i = 0; i < l / 3; i++) {
			aabb.includePoint(vertices.get(), vertices.get(), vertices.get());
		}
		vertices.rewind();
	}

	public void draw(GL2 gl) {
		// TODO Indexed rendering, drawing triangles...
		if(vertices != null && vertices.hasRemaining()){
			if(indices == null){
		   	 	gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
				// Set The Vertex Pointer To Our Vertex Data
				gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices.position(0));
				// Set The Vertex Pointer To Our TexCoord Data
				//gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, mesh.texCoords);
				gl.glDrawArrays(met, 0, numElements);
				gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);				
			}else{
				
			}
		}
	}
}